package com.lute.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.taglibs.standard.lang.jstl.GreaterThanOperator;

import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeAccountingPeriodState;
import com.lute.model.EmployeeCurrentState;
import com.lute.model.EmployeeEntry;
import com.lute.model.PositionName;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class LuteSummaryReport extends LuteAbstractFactoryReport {
	
	private String creativeHoursPercentage;
	private String hoursTotal;
	private String creativeHoursTotal;
	private List<String> category;
	private List<String> hours;
	private List<String> cretaiveHours;
	
	public LuteSummaryReport() {}
	
	public LuteSummaryReport(String reportType, String generatedOn, EmployeeAccountingPeriod period, String from) {
		
		super(reportType,generatedOn, period, from);
		category = new ArrayList<String>();
		hours = new ArrayList<String>();
		cretaiveHours = new ArrayList<String>();
	}
	
	public String getCreativeHoursPercentage() {
		return creativeHoursPercentage;
	}

	public void setCreativeHoursPercentage(String creativeHoursPercentage) {
		this.creativeHoursPercentage = creativeHoursPercentage;
	}
	
	
	public String getHoursTotal() {
		return hoursTotal;
	}

	public void setHoursTotal(String hoursTotal) {
		this.hoursTotal = hoursTotal;
	}

	public String getCreativeHoursTotal() {
		return creativeHoursTotal;
	}

	public void setCreativeHoursTotal(String creativeHoursTotal) {
		this.creativeHoursTotal = creativeHoursTotal;
	}
	
	public List<String> getCategory() {
		return category;
	}

	public void setCategory(List<String> category) {
		this.category = category;
	}

	public List<String> getHours() {
		return hours;
	}

	public void setHours(List<String> hours) {
		this.hours = hours;
	}

	public List<String> getCretaiveHours() {
		return cretaiveHours;
	}

	public void setCretaiveHours(List<String> cretaiveHours) {
		this.cretaiveHours = cretaiveHours;
	}
	
	@Override
	public JRDataSource getDataSource(List<Integer> employeeIds, List<String> froms) {
		List<LuteSummaryReport> summary = new ArrayList<LuteSummaryReport>();		
		List<GregorianCalendar> cals = new ArrayList<GregorianCalendar>();
		
		for(String cal: froms) {
			String tab[] = cal.split("-");
			cals.add(new GregorianCalendar(Integer.parseInt(tab[0]),Integer.parseInt(tab[1])-1,Integer.parseInt(tab[2])));
		}
		
		SimpleDateFormat dateFromater = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		
		for(GregorianCalendar cal: cals) {
			
			List<EmployeeAccountingPeriod> employeePeriods = EmployeeAccountingPeriod.getEmployeeAccountingPeriodsFromDB(employeeIds, cal);
			for(EmployeeAccountingPeriod period: employeePeriods) {
					float hoursTotal = 0;
					float creativeHoursTotal = 0;
					
					LuteSummaryReport report = new LuteSummaryReport(LuteReportName.SUMMARY_REPORT.toString(),
																	 dateFormat.format(date), period, dateFromater.format(cal.getTime()));
					creativeHoursPercentage = "n/a";
					if(period.getEmployee() == null) {
						if(!(period.getApprover().getApproverProfile().getPositions().getName().equals(PositionName.SUPPORT.toString()))) {
							creativeHoursPercentage = period.getApprover().getApproverProfile().
									   getPositions().getCreativity_percentage().toString();
						}
					} else {
						if(!(period.getEmployee().getEmployeeProfil().getPositions().getName().equals(PositionName.SUPPORT.toString()))) {
							creativeHoursPercentage = period.getEmployee().getEmployeeProfil().
						               getPositions().getCreativity_percentage().toString();
						}
					}
					
					List<EmployeeEntry> entries = period.getEmployeeEntries();
					for(EmployeeEntry entry: entries) {
						report.getCategory().add(entry.getCompanyCategory().getName()+"\n");
						report.getHours().add(Float.toString(entry.getHours())+"\n");
						report.getCretaiveHours().add(Float.toString(entry.getCreativeHours())+"\n");
						hoursTotal += entry.getHours();
						creativeHoursTotal += entry.getCreativeHours();
					}
					report.setCreativeHoursPercentage(creativeHoursPercentage);
					report.setCreativeHoursTotal(Float.toString(creativeHoursTotal));
					report.setHoursTotal(Float.toString(hoursTotal));
					summary.add(report);
			}
		}
		
		JRDataSource ds = new JRBeanCollectionDataSource(summary);
		
		return ds;
	}
	
}
